<?
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
		
			//load model

		$this->load->model('Mcustomer');
		


    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
	check_login();
	}

	function index($page='')
	{
	
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/customer/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mcustomer->count_all();

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mcustomer->list_data($page,$limit);

    $data['title'] = 'Danh sách Thành viên';

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'customer_review';

    $this->load->view('admin/template_main', $data);
	}
	//upddate
	function update()
	{
		$id= $this->uri->segment(4);
		 if ($this->input->post('update'))

		{
		
	
		  //set rules
	
		  $this->form_validation->set_rules('last_name', 'last_name', 'required');
	
		  $this->form_validation->set_rules('first_name', 'first_name', 'required');
	
		  $this->form_validation->set_rules('birthday', 'birthday', 'required');
	
		  $this->form_validation->set_rules('address', 'address', 'required');
		  $this->form_validation->set_rules('email', 'email', 'required|valid_email');
	
	
	
		 $this->form_validation->set_rules('phone', 'phone', 'required|numeric');

		 if ($this->form_validation->run() && $this->sub_update())
	
			redirect('admin/customer/', 'refresh');
	
		  else
			{
				
				$data['missing'] = $this->form_validation->_error_array;
				
			}
		
			}
		
			elseif ($this->input->post('close'))
		
				{
		
					redirect('admin/customer/', 'refresh');
		
				}



		 //reload template

		$data['main'] = 'customer_update';

   		 $data['title'] = 'Quản lý Thành viên';
		
		$data['result']= $this->Mcustomer->info_form_id($id);
		//echo "<pre>";
	//	echo $id;
		//print_r($data['result']);
		//echo "</pre>";
			$this->load->view('admin/template_main', $data);
	}
	
	
		function sub_update()
	{
		$id=$this->uri->segment(4);
		if(isset($_POST["update"]))
		{
			if($this->Mcustomer->check_email($this->input->post("email"),$id))
			{	
			//	echo $this->Mcustomer->check_email($this->input->post("email"),$this->uri->segment(4));
			//	echo $this->input->post("email"); echo $id;
				$this->form_validation->_error_array["email"] = "This email have been existed ."; 
				return false;
			
			}
			if(isset($_POST["member_card"]))
		{
			$this->load->model("Mcard");
			$id_card = $this->input->post("member_card");
			if( !is_numeric($id_card) || !$this->Mcard->checkId($id_card) )// ton tai id
			{
				$this->form_validation->_error_array["member_card"] = "Mã thẻ không đúng!"; 
				return false;
			}
		}
		else
			$id_card = "";
				$name = array(
				"first_name"=>htmlentities($_POST["first_name"],ENT_QUOTES,"UTF-8"),
				"last_name"=>htmlentities($_POST["last_name"],ENT_QUOTES,"UTF-8"),
				"full"=>htmlentities($_POST["first_name"],ENT_QUOTES,"UTF-8")." ". htmlentities($_POST["last_name"],ENT_QUOTES,"UTF-8"),
				);
			if(isset($_POST["password"]))
				$data1 = array(
				"email" => $_POST["email"],
				"password" => pass_md5($_POST["password"]),
				"name" => serialize($name),
				"birthday" => $this->input->post("birthday"),
				"phone" => $this->input->post("phone"),
				"sex" => $_POST["sex"],
				"address" => $_POST["address"],
				"company" => $_POST["company"],
			"tax" => $_POST["tax"],
			"member_card" => $id_card,
			);
			else
				$data1 = array(
				"email" => $_POST["email"],
			
				"name" => serialize($name),
				"birthday" => $this->input->post("birthday"),
				"phone" => $this->input->post("phone"),
				"sex" => $_POST["sex"],
				"address" => $_POST["address"],
				"company" => $_POST["company"],
			"tax" => $_POST["tax"],
			"member_card" =>$id_card,
		);
			
		
			if($this->Mcustomer->update($data1,$id))
				return true;
			else
				return false;
		}
		else
			return false;
	
	
	
	}
	
	
	
	function create()
	{
	 if ($this->input->post('create'))

    {
	

      //set rules

      $this->form_validation->set_rules('last_name', 'last_name', 'required');

      $this->form_validation->set_rules('first_name', 'first_name', 'required');

      $this->form_validation->set_rules('birthday', 'birthday', 'required');

      $this->form_validation->set_rules('address', 'address', 'required');
      $this->form_validation->set_rules('email', 'email', 'required|valid_email');

      $this->form_validation->set_rules('password', 'password', 'required');

     $this->form_validation->set_rules('phone', 'phone', 'required|numeric');

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ($this->form_validation->run() && $this->sub_create())

        redirect('admin/customer/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/customer/', 'refresh');

		}



		 //reload template

		$data['main'] = 'customer_create';

    $data['title'] = 'Quản lý Thành viên';

		$this->load->view('admin/template_main', $data);
	}
	function check_email()
	{
		$email=$this->uri->segment(4);
		if(!$this->Mcustomer->check_email($email))
			echo "1";
		else
			echo "0";

	}
	
	function sub_create()
	{
		if(isset($_POST["create"]))
		{
		if($this->Mcustomer->check_email(trim($this->input->post("email"))))
		{	
			$this->form_validation->_error_array["email"] = "Email này đã tồn tại."; 
		
			return false;
		
		}
			if(isset($_POST["member_card"]))
		{
			$this->load->model("Mcard");
			$id_card = $this->input->post("member_card");
			if( !is_numeric($id_card) || !$this->Mcard->checkId($id_card))// ton tai id
			{
				$this->form_validation->_error_array["member_card"] = "Mã thẻ không đúng!"; 
				return false;
			}
		}
		else
			$id_card = "";
		$name = array(
				"first_name"=>htmlentities($_POST["first_name"],ENT_QUOTES,"UTF-8"),
				"last_name"=>htmlentities($_POST["last_name"],ENT_QUOTES,"UTF-8"),
				"full"=>$_POST["first_name"]." ".$_POST["last_name"],
						
		
		);
		
		$data = array(
			"email" => $_POST["email"],
			"password" => pass_md5($_POST["password"]),
			"name" => serialize($name),
			"birthday" => $this->input->post("birthday"),
			"phone" => $this->input->post("phone"),
			"sex" => $_POST["sex"],
			"address" => $_POST["address"],
			"company" => $_POST["company"],
			"tax" => $_POST["tax"],
			"member_card" => $id_card,
			);
		
	
	
			
		if($this->Mcustomer->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}
	
	
	function request()

	{

		//determine 'action'
	 

		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;
				
			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mcustomer->delete();

				} break;

			}//end switch



			redirect('admin/customer/', 'refresh');

		}//end if

	}
	
}









?>
